home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_CAUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  22.7 KB  |  1,108 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_Extra.c
  3.  
  4.     Contains:    Utility routines for calling CALib.
  5.  
  6.     Written by:    Rick Badertscher
  7.  
  8.     Copyright © 1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <7>        05/15/95    RB        Added "Container" handler implementations
  13.         <6>        05/13/95    RB        Added CAUtil_IsCALibPresent()
  14.         <5>        04/23/95    RB        Added Focus handlers
  15.         <4>        04/01/95    RB        Added CAWindowActivateHandler()
  16.         <3>        03/30/95    RB        Added App_CAFocusLostProc() and App_CAFocusAquiredProc()
  17.         <2>        03/16/95    RB        Added Read/Write Wrappers and CASaveDlgHook to
  18.                                     facilitate the Bento storage model
  19.         <1>        02/21/95    RB        Created.
  20. */
  21.  
  22.  
  23. #include <Folders.h>
  24. #include <GestaltEqu.h>
  25. #include <TextServices.h>
  26.  
  27.  
  28. #ifdef USE_CALIB
  29. #include "CALib.h"
  30. #endif
  31.  
  32. #include "CAS_Globals.h"
  33. #include "CAS_App.h"
  34. #include "CAS_Content.h"
  35. #include "CAS_Doc.h"
  36. #include "CAS_Win.h"
  37. #include "CAS_App.h"
  38. #include "CAS_ToolPalette.h"
  39. #include "CAS_Dialog.h"
  40. #include "CAS_Drag.h"
  41. #include "CAS_CAUtil.h"
  42. #include "CAS_StringTools.h"
  43.  
  44. FileFilterUPP        gInsertFilterProc;
  45.  
  46. pascal Boolean CAUtil_InsertFileFilter (CInfoPBPtr pb);
  47.  
  48.  
  49. //===========================================================================
  50. #pragma mark '      Asynchronous Handlers
  51. //===========================================================================
  52.  
  53.  
  54. void App_CAErrorHandler (OSErr err, char* mesg)
  55. {
  56.  
  57.     Str255        errorMsg;
  58.     Str63        errorNumStr;
  59.     
  60.     NumToString (err, errorNumStr);
  61.     sCopyStr("\pCALib Error # ", errorMsg);
  62.     sAppendStr(errorNumStr, errorMsg);
  63.     
  64.     ParamText( errorMsg, nil, nil, nil );
  65.  
  66.     Dialog_CenterALRTonFrontWindow( kErrorDialog );
  67.     StopAlert( kErrorDialog, nil );
  68.  
  69.  
  70. }
  71.  
  72.  
  73. //----------------------------------------------------------------------------
  74. //    App_CAFrameShapeRequestHandler 
  75. //
  76.  
  77. pascal Boolean App_CAFrameShapeRequestHandler (    CADocumentRef        document,
  78.                                                 CAFrameRef            frameRef,
  79.                                                 RgnHandle            rgn)
  80. {
  81.     
  82.     Boolean            modified = false;
  83.     DocPtr            theDoc;
  84.     Rect            rect;
  85.     ElemPtr            elem;
  86.     
  87.     theDoc = CAUtil_GetDocPtr (document);
  88.     elem = Doc_FindElemForFrameRef (theDoc, frameRef);
  89.     
  90.     rect = (**rgn).rgnBBox;
  91.     
  92.     Elem_SetRect (elem, rect);
  93.     modified = true;
  94.     
  95.     return (modified);
  96.     
  97. }
  98.  
  99.  
  100. //----------------------------------------------------------------------------
  101.  
  102.  
  103. WindowPtr App_CAFrontWindowProc(
  104.     CAWindowLayer    windowLayer )
  105. {
  106.  
  107.     switch (windowLayer)
  108.     {
  109.         case kCADocumentWindow:
  110.             return App_GetFrontDocWindow();
  111.             
  112.         case kCAFloatingWindow:
  113.             return ToolPalette_VerifyWindow( nil );
  114.  
  115.         default:
  116.             break;
  117.     }
  118.  
  119.     return nil;
  120. }
  121.  
  122. //----------------------------------------------------------------------------
  123.  
  124. void App_CASelectWindowProc(
  125.     WindowPtr    window )
  126. {
  127.     if ((window == nil) || !Win_IsVisible( window ))
  128.         return;
  129.  
  130.      if (Win_IsFloater( window ))
  131.          ;
  132.     else if (Win_IsAppWindow( window ))
  133.         App_SetFrontDocWindow( window );
  134. }
  135.  
  136. //----------------------------------------------------------------------------
  137.  
  138. Boolean App_CAIsFloaterProc(
  139.     WindowPtr    window )
  140. {
  141.     return Win_IsFloater( window );
  142. }
  143.  
  144. //----------------------------------------------------------------------------
  145.  
  146. WindowPtr App_CAAllocWindowRecProc(
  147.     CAWindowLayer    windowLayer )
  148. {
  149.     switch (windowLayer)
  150.     {
  151.         case kCANoLayer:
  152.         case kCADocumentWindow:
  153.         case kCAFloatingWindow:
  154.             break;
  155.  
  156.         default:
  157.             break;
  158.     }
  159.  
  160.     return nil;
  161. }
  162.  
  163. //----------------------------------------------------------------------------
  164.  
  165. void App_CADisposeWindowRecProc(
  166.     WindowPtr    window )
  167. {
  168. }
  169.  
  170. //----------------------------------------------------------------------------
  171.  
  172. void App_CAFocusAcquiredProc ( CADocumentRef document, CAFocusType focusType)
  173. {
  174.  
  175.     DocPtr        theDoc;
  176.     GrafPtr        savePort;
  177.     WindowPtr    theWindow = NULL;
  178.     
  179.     theDoc = CAUtil_GetDocPtr (document);
  180.     theWindow = Doc_GetWindow(theDoc);
  181.     
  182.     GetPort (&savePort);
  183.     if (theWindow) SetPort (theWindow);
  184.     
  185.     switch (focusType)
  186.     {
  187.     
  188.         case kCAPrimaryFocus:
  189.         
  190.             // Save the focus document
  191.             
  192.             gFocusDocument = document;
  193.         
  194.             // Set and draw the app menu bar
  195.             
  196.             SetMenuBar (gAppMenuBar);
  197.             DrawMenuBar();
  198.  
  199.             // Invalidate selection regions
  200.             
  201.             CAUtil_InvalSelRgns (theDoc);
  202.             
  203.             break;
  204.             
  205.         default:
  206.             break;
  207.             
  208.     }
  209.     
  210.     SetPort (savePort);
  211.     
  212. }
  213.  
  214. //----------------------------------------------------------------------------
  215.  
  216. void App_CAFocusLostProc ( CADocumentRef document, CAFocusType focusType )
  217. {
  218.     DocPtr        theDoc;
  219.     GrafPtr        savePort;
  220.     WindowPtr    theWindow = NULL;
  221.     
  222.     theDoc = CAUtil_GetDocPtr (document);
  223.     theWindow = Doc_GetWindow(theDoc);
  224.     
  225.     GetPort (&savePort);
  226.     if (theWindow) SetPort (theWindow);
  227.  
  228.     switch (focusType)
  229.     {            
  230.     
  231.         case kCAPrimaryFocus:
  232.         
  233.             gFocusDocument = NULL;
  234.             //Doc_SelectAllElements (theDoc, false);
  235.             CAUtil_InvalSelRgns (CAUtil_GetDocPtr (document));
  236.             break;
  237.             
  238.         default:
  239.             break;
  240.     }
  241.     
  242.     SetPort (savePort);
  243.  
  244. }
  245.  
  246. //----------------------------------------------------------------------------
  247.  
  248. void App_CAWindowActivateHandler (WindowPtr    theWindow, Boolean activating)
  249. {
  250.  
  251.     DocPtr        theDoc;
  252.     
  253.     theDoc = (DocPtr)GetWRefCon(theWindow);
  254.     
  255.     if (activating)
  256.     {
  257.         App_Activate (theWindow);
  258.     }
  259.     else
  260.     {
  261.         App_Deactivate (theWindow);
  262.     }
  263.  
  264. }
  265.  
  266.  
  267. //----------------------------------------------------------------------------
  268.  
  269. Boolean App_CAAdjustBorderHandler (    CADocumentRef    document,
  270.                                     CAFrameRef        frameRef,
  271.                                     RgnHandle        rgn)
  272. {
  273.     ElemPtr            elem;
  274.     DocPtr            theDoc;
  275.     RgnHandle        coveredRgn;
  276.     Point            location;
  277.     
  278.     theDoc = CAUtil_GetDocPtr (document);
  279.  
  280.     // Clip the active border to any content elements above frameRef in
  281.     // the z-order
  282.     
  283.     elem = Doc_FindElemForFrameRef (theDoc, frameRef);
  284.     
  285.     // compute the covered rgn
  286.     coveredRgn = NewRgn();
  287.     ElemColl_GetRgnOverElem (theDoc->contentColl, elem, coveredRgn);
  288.     
  289.     // subtract the coveredRgn from the active border rgn
  290.     // rgn is in frame coordinates (0 based), so first transform locate it
  291.     // according to frameRef's external transform
  292.     
  293.     Elem_GetLocation (elem, &location);
  294.     OffsetRgn (rgn, location.h, location.v);
  295.     DiffRgn (rgn, coveredRgn, rgn);
  296.     
  297.     // put rgn back in frame coordinates
  298.     OffsetRgn (rgn, -location.h, -location.v);
  299.     
  300.     return (true);
  301.     
  302. }
  303.  
  304.  
  305. //===========================================================================
  306. #pragma mark '       Miscellaneous Utility Routines
  307. //===========================================================================
  308.  
  309. //----------------------------------------------------------------------------
  310.  
  311. pascal short CASaveDialogHook (short item, DialogPtr theDialog, void* userData)
  312. {
  313.  
  314.     if (item == sfHookFirstCall)
  315.     {
  316.         // Enable the radio button representing the current storage model for this
  317.         // doc
  318.         short            currentStorageModel = *((short*)userData);
  319.         ControlHandle    theControl;
  320.         
  321.         theControl = (ControlHandle) Dialog_GetItemHandle(theDialog, kNativeRadioItemID);
  322.         if (theControl != nil)
  323.             SetControlValue( theControl, (currentStorageModel == kNativeStorageModel) ? 1 : 0 );
  324.             
  325.         theControl = (ControlHandle) Dialog_GetItemHandle(theDialog, kBentoRadioItemID);
  326.         if (theControl != nil)
  327.             SetControlValue( theControl, (currentStorageModel == kBentoStorageModel) ? 1 : 0 );
  328.             
  329.     }
  330.     
  331.     if ((item == kNativeRadioItemID) || (item == kBentoRadioItemID))
  332.     {
  333.         
  334.         ControlHandle    theControl;
  335.  
  336.         theControl = (ControlHandle) Dialog_GetItemHandle(theDialog, kNativeRadioItemID);
  337.         if (theControl != nil)
  338.             SetControlValue( theControl, (item == kNativeRadioItemID) ? 1 : 0 );
  339.  
  340.         theControl = (ControlHandle)Dialog_GetItemHandle( theDialog, kBentoRadioItemID);
  341.         if (theControl != nil)
  342.             SetControlValue( theControl, (item == kBentoRadioItemID) ? 1 : 0 );
  343.  
  344.         *((short*)userData) = (item == kNativeRadioItemID) ? kNativeStorageModel :
  345.                                     kBentoStorageModel;
  346.  
  347.         return (sfHookNullEvent);
  348.         
  349.     }
  350.     
  351.     return (item);
  352.     
  353. }
  354.  
  355.  
  356. //----------------------------------------------------------------------------
  357.  
  358. OSErr CAUtil_ShutdownCALib()
  359. {
  360.     
  361.     OSErr        theErr;
  362.  
  363.     CAUnregisterWindow (ToolPalette_VerifyWindow(NULL));
  364.     CAShutdown();
  365.     if (theErr = CAError())
  366.         ;        // handle the error
  367.             
  368.     // Ignore the error. What would we be able to do about it anyway?
  369.     CloseTSMAwareApplication();
  370.  
  371.     return (theErr);
  372.  
  373. }
  374.  
  375. //----------------------------------------------------------------------------
  376.  
  377. OSErr CAUtil_InitCALib()
  378. {
  379.     TSMDocumentID        kAnyDoc = NULL;
  380.     OSErr                err = noErr;
  381.     long                gestaltResponse;
  382.     
  383.     if ((err = InitEditionPack()) != noErr)
  384.     {
  385.         DebugStr( "\pInitEditionPack failed" );
  386.     }
  387.  
  388.     // temporary. should always be around in system 7.1 and later.
  389.     if (( (err = Gestalt( gestaltTSMgrVersion, &gestaltResponse)) == noErr) &&
  390.          (gestaltResponse >= 1))
  391.          ;
  392.     else
  393.         DebugStr( "\pTSM not available. This is unexpected." );
  394.     
  395.     if ((err = InitTSMAwareApplication()) != noErr)
  396.     {
  397.         DebugStr("\pInitTSMAwareApplication failed");
  398.  
  399.         ExitToShell();
  400.     }
  401.  
  402.     if (UseInputWindow(kAnyDoc, true) != noErr)
  403.         DebugStr( "\pUseInputWindow failed. Bizarrrrrre." );
  404.  
  405.     CAInit();
  406.     
  407.     CASetErrorProc( (CAErrorProc) App_CAErrorHandler );
  408.  
  409.     if ((err = CAError()) != noErr)
  410.         ;        // handle the error
  411.  
  412.  
  413.     return (err);
  414.     
  415. }
  416.  
  417.  
  418.  
  419. //----------------------------------------------------------------------------
  420.  
  421. OSErr    CAUtil_ReadWrapper    (IOStreamPtr ioStream, Ptr buffer, long* length)
  422. {
  423.     
  424.     OSErr    theErr = noErr;
  425.     DocPtr    theDoc;
  426.     
  427.     theDoc = ioStream->theDoc;
  428.  
  429.     if (ioStream->type == fileStream)
  430.     {
  431.         if (theDoc->storageModel == kBentoStorageModel)
  432.         {
  433.             CARead(theDoc->partDocRef, buffer, (CASize*) length );
  434.             return (CAError());
  435.         }
  436.         else
  437.         {
  438.             theErr = FSRead( theDoc->fileRefNum, length, buffer);
  439.         }
  440.     }
  441.     else
  442.     {
  443.         BlockMove (ioStream->buffer, buffer, *length);
  444.         ioStream->buffer = (Ptr) ioStream->buffer + *length;
  445.  
  446.     }
  447.     
  448.     return (theErr);
  449.  
  450. }
  451.  
  452. //----------------------------------------------------------------------------
  453.  
  454. OSErr    CAUtil_WriteWrapper    (IOStreamPtr ioStream, Ptr buffer, long* length)
  455. {
  456.  
  457.     OSErr theErr = noErr;
  458.     DocPtr    theDoc;
  459.     
  460.     theDoc = ioStream->theDoc;
  461.  
  462.     if (ioStream->type == fileStream)
  463.     {
  464.         if (theDoc->storageModel == kBentoStorageModel)
  465.         {
  466.             CAWrite(theDoc->partDocRef, buffer, (CASize*) length );
  467.             return (CAError());
  468.     
  469.         }
  470.         else
  471.         {
  472.             theErr = FSWrite( theDoc->fileRefNum, length, buffer);
  473.         }
  474.     }
  475.     else
  476.     {
  477.         BlockMove (buffer, ioStream->buffer, *length);    
  478.         ioStream->buffer = (Ptr) ioStream->buffer + *length;
  479.     }
  480.     return (theErr);
  481.  
  482. }
  483.  
  484. //----------------------------------------------------------------------------
  485.  
  486. OSErr    CAUtil_WriteTo    (CADocumentRef doc, Ptr buffer, long* length)
  487. {
  488.     CAWrite(doc, buffer, (CASize*) length );
  489.     return (CAError());
  490. }
  491.  
  492. //----------------------------------------------------------------------------
  493.  
  494. Boolean    CAUtil_IsCALibPresent ()
  495. {
  496.     
  497.     Boolean    present = false;
  498.     
  499.     // As per the CALib pre-alpha code review.
  500.     
  501.     
  502.     if (CAInit != NULL)
  503.     {
  504.         present = true;
  505.     }
  506.     
  507.     return (present);
  508.     
  509. }
  510.  
  511. //---------------------------------------------------------------------------
  512. void CAUtil_PartInfoMenu (DocPtr theDoc)
  513. {
  514.     short    i, count;
  515.     ElemPtr    elem;
  516.  
  517.     count = ElemColl_GetCount (theDoc->contentColl);
  518.     
  519.     for (i=1; i <= count; i++)
  520.     {
  521.         elem = ElemColl_GetNthElem (theDoc->contentColl, i);
  522.         if (Elem_GetSelected (elem) && (Elem_GetType (elem) == kFrameElemType))
  523.         {
  524.             CAShowPartFrameInfo(theDoc->partDocRef,
  525.                 CAGetVisFrame(theDoc->partDocRef, (CAFrameRef) elem->elemData));
  526.         }
  527.     }
  528.     
  529. }
  530.  
  531.  
  532. //---------------------------------------------------------------------------
  533. void CAUtil_OpenSelectionMenu (WindowPtr theWindow)
  534. {
  535.     short                i, count;
  536.     DocPtr                theDoc;
  537.     ElemPtr                elem;
  538.  
  539.     // if there is no window or it's not ours, get out.
  540.     if (Win_IsAppWindow( theWindow ))
  541.     {
  542.         theDoc = (DocPtr)GetWRefCon( theWindow );
  543.     }
  544.  
  545.     count = ElemColl_GetCount (theDoc->contentColl);
  546.     
  547.     for (i=1; i <= count; i++)
  548.     {
  549.         elem = ElemColl_GetNthElem (theDoc->contentColl, i);
  550.         if (Elem_GetSelected (elem) && (Elem_GetType (elem) == kFrameElemType))
  551.         {
  552.             CAViewFrameInWindow(theDoc->partDocRef, (CAFrameRef) elem->elemData);
  553.         }
  554.     }
  555.  
  556.     
  557. }
  558.  
  559.  
  560.  
  561. //---------------------------------------------------------------------------
  562.  
  563. void CAUtil_InsertMenu (WindowPtr theWindow)
  564. {
  565. SFReply                sfReply;
  566. WindowPtr            frontWindow;
  567. CADocumentRef        insertDoc;
  568. DocPtr                theDoc;
  569. CADocumentRef        document;
  570. CAFrameRef            newFrameRef;
  571. Rect                dlogRect;
  572. Rect                rScreen;
  573. ElemPtr                elem;
  574.  
  575.     // if there is no window or it's not ours, get out.
  576.     if (Win_IsAppWindow( theWindow ))
  577.     {
  578.         theDoc = (DocPtr)GetWRefCon( theWindow );
  579.     }
  580.     
  581.     document = theDoc->partDocRef;
  582.  
  583.      gInsertFilterProc = NewFileFilterProc(CAUtil_InsertFileFilter);
  584.     
  585.     frontWindow = CAGetFrontDocWindow();
  586.     if (!CARequestModalFocus( frontWindow))
  587.         return;
  588.     
  589.     dlogRect.left = 0;        dlogRect.top = 0; 
  590.     dlogRect.right = 136;    dlogRect.bottom = 136; 
  591.     
  592.     GetMainScreenRect( &rScreen );
  593.     rScreen.top += GetMBarHeight() + 4;
  594.  
  595.     rCenterRectInRect( &rScreen, &dlogRect );
  596.  
  597.     SFGetFile( (*((Point*) &dlogRect)), "\p",
  598.                 (FileFilterUPP) gInsertFilterProc, -1,
  599.                 NULL,
  600.                 (DlgHookUPP) NULL,
  601.                 &sfReply);
  602.  
  603.     CARelinquishModalFocus( frontWindow );
  604.  
  605.     if (sfReply.good) 
  606.     {
  607.         CACloneKey    key;
  608.         CATransform    extTransform;
  609.         FSSpec        insertFIle;
  610.         
  611.         FSMakeFSSpec (sfReply.vRefNum, 0, sfReply.fName, &insertFIle);
  612.         
  613.         insertDoc = CAOpenDocument (&insertFIle, NULL, 0, 0);
  614.     
  615.         key = CABeginClone (insertDoc, document, kCACloneAll);
  616.         newFrameRef = CACloneFrameRef (NULL, insertDoc, document, true, key);
  617.         CAEndClone (insertDoc, document, key);
  618.         
  619.         CACloseDocument (insertDoc);
  620.         
  621.         elem = Doc_AddEmbeddedFrame (theDoc, newFrameRef, theDoc->insertionPoint);
  622.         
  623.         // Create a facet for the new frame
  624.         Elem_SetVisible (elem, true);
  625.  
  626.         Doc_SelectAllElements (theDoc, false);
  627.         Elem_SetSelected (elem, true);
  628.  
  629.         Doc_SetDirty( theDoc, true );
  630.  
  631.     }
  632.     
  633.     
  634.     return;
  635.  
  636. }
  637.  
  638.  
  639. //--------------------------------------------------------------------
  640. // CAUtil_InsertFileFilter
  641. //--------------------------------------------------------------------
  642.  
  643. pascal Boolean CAUtil_InsertFileFilter (CInfoPBPtr pb)
  644. {
  645. HFileInfo*    fpb = (HFileInfo*)pb;
  646.  
  647.     if (!(fpb->ioFlAttrib & 16))
  648.     {
  649.         if ((fpb->ioFlFndrInfo.fdCreator == kODShellSignature) &&
  650.             (fpb->ioFlFndrInfo.fdType != 'APPL') &&
  651.             (fpb->ioFlFndrInfo.fdType != 'shlb'))
  652.         {
  653.             return false;
  654.         }
  655.     }
  656.     
  657.     return true;
  658. }
  659.  
  660.  
  661.  
  662. //----------------------------------------------------------------------------
  663.  
  664. void CAUtil_InvalSelRgns (DocPtr theDoc)
  665. {
  666. short            i;
  667. short            count;
  668. ElemPtr            elem;
  669.  
  670.     count = ElemColl_GetCount (theDoc->contentColl);
  671.     for (i=1; i <= count; i++)
  672.     {
  673.     
  674.         elem = ElemColl_GetNthElem (theDoc->contentColl, i);
  675.         Elem_InvalSelRgn (elem);
  676.     }
  677.     
  678.     ElemColl_ClipElements(theDoc->contentColl, NULL);
  679.     
  680. }
  681.  
  682.  
  683. //----------------------------------------------------------------------------
  684.  
  685. DocPtr    CAUtil_GetDocPtr (CADocumentRef document)
  686. {
  687. WindowPtr    curFrontW;
  688. DocPtr        theDoc = NULL;
  689.  
  690.     curFrontW = (WindowPtr)LMGetWindowList();
  691.     while (curFrontW != nil)
  692.     {
  693.         if (!Win_IsFloater( curFrontW ) &&
  694.             Win_IsAppWindow( curFrontW) && !Win_IsToolWindow(curFrontW))
  695.         {
  696.             theDoc = (DocPtr)GetWRefCon( curFrontW );
  697.             if (theDoc->partDocRef == document) break;
  698.             theDoc = NULL;
  699.         }
  700.         
  701.         curFrontW = (WindowPtr)(((WindowPeek)curFrontW)->nextWindow);
  702.  
  703.     }
  704.  
  705.     return theDoc;
  706.  
  707.  
  708. }
  709.  
  710.  
  711. //----------------------------------------------------------------------------
  712.  
  713. Boolean CAUtil_HandleMouseDownBorder (        EventRecord*    theEvent,
  714.                                             CAEventInfo*    eventInfo)
  715. {
  716.     WindowPtr        theWindow;
  717.     short            windowCode;
  718.     DocPtr            theDoc;
  719.     Boolean            handled = false;
  720.     ElemPtr            elem;
  721.     GrafPtr            savePort;
  722.     
  723.     windowCode = FindWindow( theEvent->where, &theWindow );
  724.     
  725.     GetPort (&savePort);
  726.     SetPort (theWindow);
  727.     
  728.     if (windowCode == inContent)
  729.     {
  730.         theDoc = (DocPtr)GetWRefCon( theWindow );
  731.  
  732.         elem = Doc_FindElemForFrameRef (theDoc, eventInfo->embeddedFrame);
  733.         
  734.         if (elem)
  735.         {
  736.         
  737.             CARequestStandardFocusSet(theWindow);
  738.         
  739.             // Bring to front and select
  740.             ElemColl_MoveElemToFront(theDoc->contentColl, elem);
  741.             Doc_SelectAllElements( theDoc, false );
  742.             Elem_SetSelected (elem, true);
  743.                 
  744.             // Redraw
  745.             Win_Update (theWindow);
  746.             
  747.             // Do a drag
  748.             Drag_DragItem( theWindow, theEvent);
  749.  
  750.  
  751.         }
  752.  
  753.         handled = true;
  754.             
  755.  
  756.     }
  757.  
  758.     SetPort (savePort);
  759.  
  760.     return (handled);
  761.  
  762. }
  763.  
  764.  
  765. //----------------------------------------------------------------------------
  766.  
  767. Boolean CAUtil_HandleMouseDownEmbedded (    EventRecord*    theEvent,
  768.                                             CAEventInfo*    eventInfo)
  769. {
  770.     WindowPtr        theWindow;
  771.     Point            docPt;
  772.     short            windowCode;
  773.     short            elemCode;
  774.     DocPtr            theDoc;
  775.     Boolean            handled = false;
  776.     ElemPtr            elem;
  777.     Point            saveOrigin;
  778.     GrafPtr            savePort;
  779.  
  780.     windowCode = FindWindow( theEvent->where, &theWindow );
  781.     theDoc = ((DocPtr)GetWRefCon(theWindow));
  782.     
  783.     GetPort( &savePort );
  784.     SetPort( theWindow );
  785.  
  786.     // Compute the mouse in doc coords.
  787.     docPt = theEvent->where;
  788.     GetOrigin( &saveOrigin );
  789.     Doc_SetGrafOrigin( theDoc );
  790.     GlobalToLocal( &docPt );
  791.     SetOrigin( saveOrigin.h, saveOrigin.v );
  792.  
  793.     if (windowCode == inContent)
  794.     {
  795.  
  796.         elemCode = ElemColl_FindElem(theDoc->contentColl, docPt, &elem);
  797.  
  798.         if (elem)
  799.         {
  800.     
  801.             switch (elemCode)
  802.             {
  803.         
  804.                 case inElement:
  805.                     
  806.                     Elem_HandleMouseDown (elem, theDoc, theEvent);
  807.                     SetRectRgn( gMouseRgn, 0, 0, 1, 1 );
  808.                     handled = true;
  809.                     break;
  810.                 case inCornerResize:
  811.                     break;
  812.                 case inEdgeResize:
  813.                     break;
  814.                     
  815.             }
  816.         
  817.         }
  818.  
  819.                     
  820.     }
  821.  
  822.     SetPort( savePort );
  823.  
  824.     return (handled);
  825.  
  826.  
  827. }
  828.  
  829. //----------------------------------------------------------------------------
  830. Boolean CAUtil_HandleMouseUpEmbedded (        EventRecord*    theEvent,
  831.                                             CAEventInfo*    eventInfo)
  832. {
  833.     WindowPtr        theWindow;
  834.     short            windowCode;
  835.      Boolean            handled = false;
  836.  
  837.     windowCode = FindWindow( theEvent->where, &theWindow );
  838.     
  839.     if (theWindow != App_GetFrontDocWindow())
  840.     {
  841.         App_SetFrontDocWindow (theWindow);
  842.         handled = true;
  843.     }
  844.  
  845.     return (handled);
  846.  
  847.  
  848. }
  849.  
  850.  
  851.  
  852. //---------------------------------------------------------------------------
  853. void CAUtil_EmbedODContainerFromCADocument (DocPtr theDoc, CADocumentRef document, Point location)
  854. {
  855.     CACloneKey            key;
  856.     CAFrameRef            newFrameRef;
  857.     CATransform            extTransform;
  858.     ElemPtr                elem;
  859.     Rect                theRect;
  860.     RgnHandle            theRgn;
  861.     Point                adjustedLocation;
  862.  
  863.     // Clone the root part from the container doc
  864.     
  865.     key = CABeginClone (document, theDoc->partDocRef, kCACloneAll);
  866.     newFrameRef = CACloneFrameRef (NULL, document, theDoc->partDocRef, true, key);
  867.     CAEndClone (document, theDoc->partDocRef, key);
  868.     
  869.     Doc_SelectAllElements (theDoc, false);
  870.     
  871.     elem = Doc_AddEmbeddedFrame (theDoc, newFrameRef, location);
  872.         
  873.     // Create a facet for the new frame
  874.     
  875.     Elem_SetVisible (elem, true);
  876.     
  877.     ElemColl_MoveElemToFront(theDoc->contentColl, elem);
  878.     Doc_SelectAllElements (theDoc, false);
  879.     Elem_SetSelected (elem, true);
  880.  
  881.     Doc_SetDirty( theDoc, true );
  882.  
  883.  
  884. }
  885.  
  886. //---------------------------------------------------------------------------
  887. Boolean CAUtil_ScrapHasType (ResType type, long* scrapOffset, long* length)
  888. {
  889. long        scrapLength;
  890. Boolean        retval = false;
  891.  
  892.     scrapLength = GetScrap( NULL, type, scrapOffset );
  893.     *length = scrapLength;
  894.  
  895.     if (scrapLength >= 0)
  896.     {
  897.         retval = true;
  898.     }
  899.     
  900.     return (retval);
  901.  
  902. }
  903.  
  904.  
  905.  
  906.  
  907. void CAUtil_CreateDocFileFromSelection (DocPtr theDoc, FSSpec* fsSpec)
  908. {
  909.     CACloneKey        key            = NULL;
  910.     OSErr            theErr        = noErr;
  911.     CADocumentRef    newDoc;
  912.     short            count, i;
  913.     ElemPtr            elem;
  914.     DocPtr            tempDoc;
  915.     FInfo            fndrInfo;
  916.     CAFrameRef        newFrameRef;
  917.  
  918.     // Create a new CASample document
  919.     tempDoc = (DocPtr)NewPtrClear( (long)sizeof(DocRecord) );
  920.     Doc_InitData( tempDoc );
  921.  
  922.     FSpCreate (fsSpec, kOurFileCreator, kContainerFileType, 0 );
  923.     tempDoc->fileSpec = *fsSpec;
  924.  
  925.     // Create the CADocument (Don't bind to a file until save time)
  926.     newDoc = CACreateDocument(NULL, NULL);
  927.     
  928.     // Set tempDoc's CADocument reference
  929.     tempDoc->partDocRef = newDoc;
  930.     tempDoc->storageModel = kBentoStorageModel;
  931.  
  932.     // Clone each selected element
  933.     
  934.     count = ElemColl_GetCount (theDoc->contentColl);
  935.     
  936.     for (i=1; i <= count; i++)
  937.     {
  938.         elem = ElemColl_GetNthElem (theDoc->contentColl, i);
  939.         if (Elem_GetSelected (elem))
  940.         {
  941.             ElemPtr    clonedElem;
  942.             
  943.             // Add the element
  944.             clonedElem = Elem_Clone (elem);
  945.             ElemColl_AddElem (tempDoc->contentColl, clonedElem);
  946.             
  947. #ifdef USE_CALIB
  948.             // Clone the frame if the element is a frame
  949.             if (elem->elemType == kFrameElemType)
  950.             {
  951.     
  952.                 if (!key)
  953.                     key = CABeginClone (theDoc->partDocRef, tempDoc->partDocRef, kCACloneCopy);
  954.     
  955.                 newFrameRef = CACloneFrameRef ((CAFrameRef)elem->elemData, theDoc->partDocRef,
  956.                     tempDoc->partDocRef, false, key);
  957.                 
  958.                 clonedElem->elemData = (Handle) newFrameRef;
  959.     
  960.             }
  961. #endif
  962.  
  963.         }
  964.     }
  965.     
  966. #ifdef USE_CALIB
  967.     // End the clone
  968.     if (key)
  969.         CAEndClone (theDoc->partDocRef, tempDoc->partDocRef, key);
  970. #endif
  971.  
  972.     Doc_WriteFile (tempDoc);
  973.     Doc_Close( tempDoc );
  974.  
  975.     // close the file (if it exists)
  976.     if (tempDoc->fileRefNum != 0)
  977.         theErr = FSClose( tempDoc->fileRefNum );
  978.     
  979.     DisposePtr ((Ptr)tempDoc);
  980.  
  981.     return;
  982.  
  983.  
  984. }
  985.  
  986.  
  987. OSErr CAUtil_AddItemsFromDocFile(
  988.         DocPtr            theDoc,
  989.         FSSpecPtr        fsSpec,
  990.         Point*            origin)
  991. {
  992.  
  993.     CACloneKey        key            = NULL;
  994.     CADocumentRef    sourceDoc;
  995.     DocPtr            tempDoc;
  996.     OSErr            err;
  997.     short            count, i;
  998.     ElemPtr            elem, newElem;
  999.     CAFrameRef        newFrameRef;
  1000.     Point            location;
  1001.     ElemCollPtr        tempColl;
  1002.     Rect            boundsRect;
  1003.     
  1004.     // Create list to hold transfer elemPtr's
  1005.     tempColl = (ElemCollPtr) NewPtr (sizeof (ElemColl));
  1006.     ElemColl_Init (tempColl, NULL);
  1007.  
  1008.     // Create a new CASample document
  1009.     tempDoc = (DocPtr)NewPtrClear( (long)sizeof(DocRecord) );
  1010.     Doc_InitData( tempDoc );
  1011.  
  1012.     // Setup for the read
  1013.     tempDoc->fileSpec = *fsSpec;
  1014.  
  1015.     err = FSpOpenDF( fsSpec, fsRdWrPerm, &tempDoc->fileRefNum );
  1016.     if (err == noErr)
  1017.         tempDoc->fileLocked = false;
  1018.     else if (err == permErr)
  1019.     {
  1020.         // open the data fork (read-only)
  1021.         tempDoc->fileLocked = true;
  1022.         err = FSpOpenDF( fsSpec, fsRdPerm, &tempDoc->fileRefNum );
  1023.     }
  1024.  
  1025.     // Read it in
  1026.     err = Doc_ReadFile( tempDoc );
  1027.     if (err) DebugStr ("\pError Doc_ReadFile");
  1028.     
  1029.     // Compute the bounds rect of the content
  1030.     Doc_CalcContentBounds(tempDoc, &boundsRect);
  1031.  
  1032.     // Copy each element from tempDoc into theDoc
  1033.     count = ElemColl_GetCount (tempDoc->contentColl);
  1034.     
  1035.     for (i=1; i<= count; ++i)
  1036.     {
  1037.     
  1038.         // Clone the content element
  1039.         elem = ElemColl_GetNthElem (tempDoc->contentColl, i);
  1040.         newElem = Elem_Clone (elem);
  1041.         ElemColl_AddElem (theDoc->contentColl, newElem);
  1042.         ElemColl_AddElemTemp (tempColl, newElem);
  1043.  
  1044. #ifdef USE_CALIB
  1045.         // Clone the CAFrame if needed
  1046.         if (newElem->elemType == kFrameElemType)
  1047.         {
  1048.  
  1049.             if (!key)
  1050.                 key = CABeginClone (tempDoc->partDocRef, theDoc->partDocRef, kCACloneCopy);
  1051.  
  1052.             newFrameRef = CACloneFrameRef ((CAFrameRef)elem->elemData, tempDoc->partDocRef,
  1053.                 theDoc->partDocRef, false, key);
  1054.             
  1055.             newElem->elemData = (Handle) newFrameRef;
  1056.  
  1057.         }
  1058. #endif
  1059.  
  1060.         // Set the new location
  1061.         Elem_GetLocation (newElem, &location);
  1062.         if (origin)
  1063.         {
  1064.             location.h = location.h - boundsRect.left + (*origin).h;
  1065.             location.v = location.v - boundsRect.top + (*origin).v;
  1066.         }
  1067.         Elem_SetLocation (newElem, location);
  1068.  
  1069.     }
  1070.  
  1071.     
  1072. #ifdef USE_CALIB
  1073.     // End the clone
  1074.     if (key)
  1075.         CAEndClone (tempDoc->partDocRef, theDoc->partDocRef, key);
  1076. #endif
  1077.  
  1078.     // Close the document
  1079.     Doc_Close( tempDoc );
  1080.  
  1081.     // close the file (if it exists)
  1082.     if (tempDoc->fileRefNum != 0)
  1083.         err = FSClose( tempDoc->fileRefNum );
  1084.     
  1085.     DisposePtr ((Ptr)tempDoc);
  1086.     
  1087.     count = ElemColl_GetCount (tempColl);
  1088.     for (i = 1; i<= count; ++i)
  1089.     {
  1090.         elem = ElemColl_GetNthElem (tempColl, i);
  1091.         
  1092.         // Set visible and select
  1093.         Elem_SetVisible (elem, true);
  1094.         Elem_SetSelected (elem, true);
  1095.     }
  1096.     
  1097.     ElemColl_Free (tempColl);
  1098.     DisposePtr ((Ptr) tempColl);
  1099.     
  1100.     Doc_SetDirty( theDoc, true );
  1101.  
  1102.     return (err);
  1103.  
  1104. }
  1105.  
  1106.  
  1107.  
  1108.